const app = getApp()
const time = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    isCard: true,
    momentsList: [],
    commentsList: [],
    strategyList: [],
    isLoading: false,                    // 判断是否尚在加载中
    article: {}                     // 内容数据
  },
  tabSelect(e) {
    const _this = this
    console.log(e)
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    if (this.data.TabCur == 0) {
      wx.cloud.callFunction({
        name: 'getAllMoments'
      }).then(res => {
        _this.setData({
          loadModal: true
        })
        console.log(res)
        for (let i = 0; i < res.result.res.data.length; i++) {
          res.result.res.data[i]._createTime = time.formatTimeTwo(res.result.res.data[i]._createTime, 'Y/M/D h:m:s')
        }
        _this.setData({
          momentsList: res.result.res.data,
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    } else if (this.data.TabCur == 1) {
      _this.setData({
        loadModal: true
      })
      wx.cloud.callFunction({
        name: 'getAllComments'
      }).then(res => {
        console.log(res)
        for (let i = 0; i < res.result.res.data.length; i++) {
          res.result.res.data[i]._createTime = time.formatTimeTwo(res.result.res.data[i]._createTime, 'Y/M/D h:m:s')
        }
        _this.setData({
          commentsList: res.result.res.data,
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    } else if (this.data.TabCur == 2) {
      _this.setData({
        loadModal: true
      })
      wx.cloud.callFunction({
        name: 'getAllStrategy'
      }).then(res => {
        for (let i = 0; i < res.result.res.data.length; i++) {
          let result = app.towxml(res.result.res.data[i].content, 'markdown', {
            base: 'https://xxx.com',             // 相对资源的base路径
            theme: 'dark',                   // 主题，默认`light`
            events: {                    // 为元素绑定的事件方法
              tap: (e) => {
                console.log('tap', e);
              }
            }
          });
          res.result.res.data[i].content = result
        }

        _this.setData({
          strategyList: res.result.res.data,
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this
    if (this.data.TabCur == 0) {
      _this.setData({
        loadModal: true
      })
      wx.cloud.callFunction({
        name: 'getAllMoments'
      }).then((res) => {
        console.log(res)
        for (let i = 0; i < res.result.res.data.length; i++) {
          res.result.res.data[i]._createTime = time.formatTimeTwo(res.result.res.data[i]._createTime, 'Y/M/D h:m:s')
        }
        _this.setData({
          momentsList: res.result.res.data
        })
        _this.setData({
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const _this = this
    if (this.data.TabCur == 0) {
      _this.setData({
        loadModal: true
      })
      wx.cloud.callFunction({
        name: 'getAllMoments'
      }).then(res => {
        for (let i = 0; i < res.result.res.data.length; i++) {
          res.result.res.data[i]._createTime = time.formatTimeTwo(res.result.res.data[i]._createTime, 'Y/M/D h:m:s')
        }
        _this.setData({
          momentsList: res.result.res.data
        })
        _this.setData({
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    } else if (this.data.TabCur == 1) {
      _this.setData({
        loadModal: true
      })
      wx.cloud.callFunction({
        name: 'getAllComments'
      }).then(res => {
        for (let i = 0; i < res.result.res.data.length; i++) {
          res.result.res.data[i]._createTime = time.formatTimeTwo(res.result.res.data[i]._createTime, 'Y/M/D h:m:s')
        }
        _this.setData({
          commentsList: res.result.res.data
        })
        _this.setData({
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    } else if (this.data.TabCur == 2) {
      _this.setData({
        loadModal: true
      })
      wx.cloud.callFunction({
        name: 'getAllStrategy'
      }).then(res => {
        for (let i = 0; i < res.result.res.data.length; i++) {
          let result = app.towxml(res.result.res.data[i].content, 'markdown', {
            base: 'https://xxx.com',             // 相对资源的base路径
            theme: 'dark',                   // 主题，默认`light`
            events: {                    // 为元素绑定的事件方法
              tap: (e) => {
                console.log('tap', e);
              }
            }
          });
          res.result.res.data[i].content = result
        }

        _this.setData({
          strategyList: res.result.res.data
        })
        _this.setData({
          loadModal: false
        })
      }).catch(err => {
        console.log(err)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  ViewImage(e) {
    wx.previewImage({
      urls: e.currentTarget.dataset.urls,
      current: e.currentTarget.dataset.url
    });
    console.log(e)
  },
  loadModal() {
    this.setData({
      loadModal: true
    })
  },
  toDesc: function(e)
  {
    const _id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/culture/strategy/strategy?_id=' + _id,
    })
  },
  toDiscuss: function(e)
  {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/culture/topic/topic?id=' + id,
    })
  }
})